<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWKeyValueType
 *
 * @author I062261
 */
class BWKeyValueType extends BWInputType {

    protected $inputableList = array();

    public function __construct($item = NULL, $inputMedia = NULL) {
        parent::__construct($item, $inputMedia);
    }

    //put your code here
    //this fuction is used for get inputable values, such as enum input type, can get enum data
    public function getInputableList($nameList = false, $data = NULL) {
        if ($this->inputableList == NULL) {
            $this->loadInputableList();
        }

        $list = array();
        $list2 = array();

        foreach ($this->inputableList as $id => $att) {
            $list[$id] = $att->getValue();
            $list2[$att->getName()] = $att->getValue();
        }
        if ($nameList)
            return $list2;

        return $list;


        //return $this->inputableList;
    }

    public function getNameList($context = NULL, $data = NULL) {
        if ($this->inputableList == NULL) {
            $this->loadInputableList();
        }

        $list = array();

        foreach ($this->inputableList as $id => $att) {
            $list[$id] = $att->getName();
        }
        return $list;
    }

    public function getInputableByName($name, $data = NULL) {
        if (is_string($name)) {
            $list = $this->getInputableList(TRUE);
            return $list[$name];
        }
    }

    public function getInputableById($id, $data = NULL) {
        if (is_numeric($id) && $id != NULL) {
            $list = $this->getInputableList();
            return $list[$id];
        }
    }

    public function addInputable($name, $value, $data = NULL) {
        if (is_string($name)) {
            $name = 'InputableValue.' . $name;
            $inputable = $this->getAttributeNode()->addAttribute($name, $value);
            //reload
            $this->loadInputableList(TRUE);
            return $inputable;
        }
    }

    public function removeInputable($key, $data = NULL) {
        $this->getAttributeNode()->removeAttribute($key);
    }

    public function loadInputableList($reload = FALSE, $data = NULL) {
        if ($reload || $this->inputableList == NULL) {
            $this->inputableList = array();
            $inputableValueNode = $this->getAttributeNode()->getAttribute('InputableValue');
            if ($inputableValueNode instanceof BWInputAttribute) {
                $children = $inputableValueNode->getChildren();
                $this->inputableList = $children;
            }
        }
        return $this->inputableList;
    }

    public function addMediaByInputableValue($media, $inputableName = NULL) {

        if ($media instanceof BWMedia) {
            $media->name = $media->name . '_' . $inputableName;
            $this->getMediaManager()->addMedia($media);
        }
    }

    public function getMediaByInputableValue($identity, $inputableName = NULL) {
        if (is_string($identity)) {
            $mediaName = $identity . '_' . $inputableName;
            $media = $this->getMediaManager()->getMedia($mediaName);
            $media->name = $identity;
            return $media;
        }
    }

    public function getMediaListByInputableValue($inputableName = NULL) {
        $mediaList = $this->getMediaManager()->getMediaList();

        $theList = array();
        if ($inputableName != NULL) {
            foreach ($mediaList as $media) {
                $mediaName = $media->name;
                $length = strlen($mediaName);
                $length2 = strlen($inputableName);
                $subStr = substr($mediaName, $length - $length2 - 1, $length2 + 1);
                if ($subStr == '_' . $inputableName) {
                    array_push($theList, $media);
                }
            }
        }
        return $theList;
    }

    public function populateInputTypeByPostData($postData = NULL, $inputIndex = NULL) {
        parent::populateInputTypeByPostData($postData, $inputIndex);
        //populate inputable
        $this->populateInputableForInputType($postData, $inputIndex);
        //populate inputable media
        $this->populateMediaForInputable($postData, $inputIndex);
        return $this;
    }

    protected function populateInputableForInputType($postData, $inputIndex = NULL) {
        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if (is_string($index)) {
                $splitIndexList = explode('_', $index);
                $count = count($splitIndexList);
                $inputableName = $index;
                if ($count > 1) {
                    $inputableName = $splitIndexList[0];
                    $index = $splitIndexList[$count - 1];
                }
            }
            if (strtolower($class) == 'inputable') {
                if ($inputIndex != NULL && $index != NULL && $index == $inputIndex) {
                    $inputMediaName = $data['name'];
                    if ($inputableName != NULL) {
                        $this->addInputable($inputableName, $value);
                    }
                } else {
                    $inputMediaName = $data['name'];
                    if ($inputableName != NULL) {
                        $this->addInputable($inputableName, $value);
                    }
                }
            }
        }
    }

    protected function populateMediaForInputable($postData, $inputIndex = NULL) {
        //parse media belong to inputable value
        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            $inputClass = NULL;
            $indexInputMediaName = NULL;
            $indexInputableName = NULL;
            if (is_string($index)) {
                $splitIndexList = explode('_', $index);
                $index = NULL;
                $count = count($splitIndexList);
                if ($count > 2) {
                    $inputClass = $splitIndexList[0];
                    $indexInputMediaName = $splitIndexList[1];
                    $indexInputableName = $splitIndexList[2];
                }
                if ($count > 3) {
                    $index = $splitIndexList[$count - 1];
                }
            }
            if ($class == 'media') {
                //save string of media
                $media = NULL;

                if ($index != NULL) {
                    if ($inputClass == 'inputable' && $indexInputMediaName == $this->inputMedia->name) {
                        if (property_exists('BWMedia', 'name') && $type == 'string' && $indexInputableName == $inputIndex) {
                            $media = new BWMedia ();
                            $media->name = $name;
                            $media->content = $value;
                            //array_push($list, $media);
                        }
                        //save image of media
                        if (property_exists('BWMedia', 'name') && $type == 'image' && $indexInputableName == $inputIndex) {
                            $media = new BWMedia ();
                            $media->name = $name;
                            $media->type = $type;
                            $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . '_' . $index . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
                            //array_push($list, $media);
                        }
                    }

                    if ($media instanceof BWMedia) {
                        if ($indexInputableName != NULL)
                            $this->addMediaByInputableValue($media, $indexInputableName);
                    }
                }
            }
        }
    }

    public function isAvailable() {
        parent::isAvailable();

        $inputableList = $this->getInputableList();

        foreach ($inputableList as $inputable) {
            $count = $this->inputMedia->getInputValueCount(array('value' => $inputable->getValue()));
            if ($count < $inputable->getLimit()) {
                return TRUE;
            }
        }

        return FALSE;
    }

}
